﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	public partial class AddStyle : System.Windows.Forms.Form
	{
		public AddStyle(ListBox listbox, string text, DataGridView dgv, int index)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.lb = listbox;
			this.text = text;
			this.Text = text;
			this.dgv = dgv;
			this.n = index;
		}

		public AddStyle(ListBox listbox, string text)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.lb = listbox;
			this.text = text;
			this.Text = text;
		}

		public AddStyle(ListBox listbox, string text, List<bigStruct> newBig, DataGridView dataGridView1)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.lb = listbox;
			this.text = text;
			this.Text = text;
			this.newbig = newBig;
			this.dgv = dataGridView1;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (this.textBox1.Text != "")
			{
				string text = this.textBox1.Text.Trim();
				this.textBox1.Text = text;
				if (this.text == "复制")
				{
					if (this.textBox1.Text != "" && this.regex3.IsMatch(this.textBox1.Text.Trim()))
					{
						if (this.lb.Items.Contains(this.textBox1.Text.Trim()))
						{
							YJKMessageBox.Show("该样式名称已存在，请重新创建！");
						}
						else
						{
							List<systemTypeStruct> list = new List<systemTypeStruct>();
							foreach (bigStruct bigStruct in SystemTypeManageForm.bigList)
							{
								if (bigStruct.style == this.lb.SelectedItem.ToString())
								{
									list.AddRange(bigStruct.DuctSystemStruct);
									list.AddRange(bigStruct.PipeSystemStruct);
								}
							}
							this.lb.Items.Add(this.textBox1.Text.Trim());
							bigStruct bigStruct2 = new bigStruct();
							bigStruct2.style = this.textBox1.Text.Trim();
							new SystemManageHelper().SaveBigList(bigStruct2, list);
							SystemTypeManageForm.bigList.Add(bigStruct2);
							this.lb.SelectedIndex = this.lb.Items.Count - 1;
							base.Close();
						}
					}
					else
					{
						YJKMessageBox.Show("请确保输入不为空！");
						this.textBox1.SelectAll();
					}
				}
				if (this.text == "修改")
				{
					if (this.textBox1.Text != null && this.regex3.IsMatch(this.textBox1.Text.Trim()))
					{
						if (this.lb.Items.Contains(this.textBox1.Text.Trim()))
						{
							YJKMessageBox.Show("该样式名称已存在，请重新创建！");
						}
						else
						{
							foreach (bigStruct bigStruct3 in SystemTypeManageForm.bigList)
							{
								if (bigStruct3.style == this.lb.SelectedItem.ToString())
								{
									bigStruct3.style = this.textBox1.Text.Trim();
									break;
								}
							}
							SystemTypeManageForm.systemFirstStyle = this.textBox1.Text.Trim();
							this.lb.BeginUpdate();
							this.lb.Items.Insert(this.n, this.textBox1.Text.Trim());
							this.lb.SelectedItem = this.textBox1.Text.Trim();
							this.lb.Items.RemoveAt(this.n + 1);
							this.lb.EndUpdate();
							base.Close();
						}
					}
					else
					{
						YJKMessageBox.Show("请确保输入不为空！");
						this.textBox1.SelectAll();
					}
				}
				if (!(this.text == "导入系统管理样式"))
				{
					return;
				}
				try
				{
					List<string> list2 = new List<string>();
					foreach (bigStruct bigStruct4 in SystemTypeManageForm.bigList)
					{
						list2.Add(bigStruct4.style);
					}
					this.newbig.ElementAt(0).style = this.textBox1.Text.Trim().ToString();
					if (list2.Contains(this.newbig.ElementAt(0).style))
					{
						if (MessageBox.Show("添加样式中有重名，是否覆盖？", "盈建科提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
						{
							if (this.newbig.ElementAt(0).style == this.lb.SelectedItem.ToString())
							{
								if (this.lb.SelectedItem.ToString() == "当前样式")
								{
									this.lb.SelectedItem = "盈建科样式";
								}
								else if (this.lb.SelectedItem.ToString() == "盈建科样式")
								{
									this.lb.SelectedItem = "当前样式";
								}
								else
								{
									this.lb.SelectedItem = "当前样式";
								}
							}
							foreach (bigStruct bigStruct5 in SystemTypeManageForm.bigList)
							{
								if (bigStruct5.style == this.newbig.ElementAt(0).style)
								{
									SystemTypeManageForm.bigList.Remove(bigStruct5);
									break;
								}
							}
							SystemTypeManageForm.bigList.Add(this.newbig.ElementAt(0));
							this.lb.SelectedItem = this.newbig.ElementAt(0).style;
							base.Close();
						}
					}
					else
					{
						SystemTypeManageForm.bigList.Add(this.newbig.ElementAt(0));
						this.lb.Items.Add(this.newbig.ElementAt(0).style);
						this.lb.SelectedItem = "当前样式";
						this.lb.SelectedItem = this.newbig.ElementAt(0).style;
						base.Close();
					}
					return;
				}
				catch
				{
					YJKMessageBox.Show("请导入正确的数据文件！");
					return;
				}
			}
			YJKMessageBox.Show("请确保输入不为空！");
		}

		private void AddStyle_Load(object sender, EventArgs e)
		{
			if (this.text == "复制")
			{
				this.textBox1.Text = this.lb.SelectedItem.ToString() + "[1]";
			}
			if (this.text == "修改")
			{
				this.textBox1.Text = this.lb.SelectedItem.ToString();
			}
			if (this.text == "导入系统管理样式" && this.newbig.Count > 0)
			{
				this.textBox1.Text = this.newbig.ElementAt(0).style;
			}
			base.CenterToScreen();
		}

		private void textBox1_Validated(object sender, EventArgs e)
		{
		}

		private ListBox lb;

		private string text;

		private DataGridView dgv;

		private int n;

		private List<bigStruct> newbig;

		private Regex regex = new Regex("^[\\u4e00-\\u9fa5_a-zA-Z0-9]+$");

		private Regex regex3 = new Regex("\\S");
	}
}
